package com.fzkj.project.controller;


import cn.hutool.core.collection.CollUtil;
import com.fzkj.common.annotation.Log;
import com.fzkj.common.core.controller.BaseController;
import com.fzkj.common.core.domain.AjaxResult;
import com.fzkj.common.core.domain.entity.SysUser;
import com.fzkj.common.core.page.TableDataInfo;
import com.fzkj.common.enums.BusinessType;
import com.fzkj.common.exception.ServiceException;
import com.fzkj.common.utils.poi.ExcelUtil;
import com.fzkj.project.entiy.ProUser;
import com.fzkj.project.entiy.ProXx;
import com.fzkj.project.service.IProUserService;
import com.fzkj.project.service.IProXxService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.rmi.ServerException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户管理Controller
 * 
 * @author zhangmx
 * @date 2024-05-10
 */
@RestController
@RequestMapping("/project/prouser")
public class ProUserController extends BaseController
{
    @Autowired
    private IProUserService proUserService;

//    @Autowired
//    private ISysUserAuthService iSysUserAuthService;

    @Autowired
    private IProXxService iProXxService;

//    /**
//     * 查询用户管理列表
//     */
//    @PreAuthorize("@ss.hasPermi('project:prouser:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(ProUser proUser)
//    {
//        SysUser user = ShiroUtils.getSysUser();
//        if (!SecurityUtils.isAdmin(user.getUserId())){
//            SysUserAuth sysUserAuthPram = new SysUserAuth();
//            sysUserAuthPram.setUserId(user.getUserId());
//            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
//            List<String> bjs = auths.stream().filter(auth->auth.getType().equals("bj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
//            proUser.setBjIds(bjs);
//        }
//        startPage();
//        List<ProUser> list = proUserService.selectProUserList(proUser);
//        return getDataTable(list);
//    }

    /**
     * 查询用户管理列表
     */
    //@PreAuthorize("@ss.hasPermi('project:prouser:list')")
    @GetMapping("/listTotal")
    public TableDataInfo listTotal(ProUser proUser)
    {
        List<ProUser> list = proUserService.selectProUserList(proUser);
        return getDataTable(list);
    }

    /**
     * 导出用户管理列表
     */
    @RequiresPermissions("project:prouser:export")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProUser proUser)
    {
        List<ProUser> list = proUserService.selectProUserList(proUser);
        ExcelUtil<ProUser> util = new ExcelUtil<ProUser>(ProUser.class);
        util.exportExcel(response, list, "用户管理数据");
    }

    /**
     * 获取用户管理详细信息
     */
    @RequiresPermissions("project:prouser:query")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") String userId)
    {
        return success(proUserService.selectProUserByUserId(userId));
    }

    /**
     * 新增用户管理
     */
    @RequiresPermissions("project:prouser:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProUser proUser) throws ServerException
    {
        List<ProXx>  proXys = iProXxService.selectProXxList(new ProXx());
        if (CollUtil.isEmpty(proXys)){
            throw new ServerException("学校积分上限未设置");
        }
        proUser.setXxId(proXys.get(0).getId());
        ProUser user = new ProUser();
        user.setStNumber(proUser.getStNumber());
        List<ProUser> list = proUserService.selectProUserList(user);
        if (CollUtil.isNotEmpty(list)){
            throw new ServiceException("对不起，您输入的学号重复[" +proUser.getStNumber() + "]");
        }
        return toAjax(proUserService.insertProUser(proUser));
    }

    /**
     * 修改用户管理
     */
    //@PreAuthorize("@ss.hasPermi('project:prouser:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProUser proUser)
    {
        ProUser user = new ProUser();
        user.setStNumber(proUser.getStNumber());
        List<ProUser> list = proUserService.selectProUserByNumberNotUserId(user);
        if (CollUtil.isNotEmpty(list)){
            throw new ServiceException("对不起，您输入的学号重复[" +proUser.getStNumber() + "]");
        }
        return toAjax(proUserService.updateProUser(proUser));
    }

    /**
     * 修改用户管理
     */
    @RequiresPermissions("project:prouser:edit")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPassword")
    public AjaxResult resetPassword(@RequestBody ProUser proUser)
    {
        return toAjax(proUserService.resetPassword(proUser));
    }

    /**
     * 删除用户管理
     */
    @RequiresPermissions("project:prouser:remove")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable String[] userIds)
    {
        return toAjax(proUserService.deleteProUserByUserIds(userIds));
    }

    @PostMapping("/getCountCjByUser")
    public AjaxResult getCountCjByUser(@RequestBody Map<String,Object> map, HttpServletRequest request){
        return proUserService.getCountCj(map);
    }
}
